package me.numark.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.R;
import me.numark.activities.MarkDetailActivity;
import me.numark.activities.ProfileActivity;
import me.numark.model.Feed;
import me.numark.model.FeedListResponse;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.Utils;
import me.numark.view.BaseViewHolder;
import me.numark.view.CircleImageView;

/**
 * Created by Evan on 15/8/8.
 */
public class NoticeCommentFragment extends BaseListFragment {
    @Override
    protected void addDataToList(BaseResponse response, boolean isNew) {

    }

   /* SimpleAdapter mCommentAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataList = new ArrayList();
        mCommentAdapter = new SimpleAdapter(this, dataList);
        mAdapter = mCommentAdapter;
    }

    @Override
    public void loadMore(int itemsCount, int maxLastVisiblePosition) {
        super.loadMore(itemsCount, maxLastVisiblePosition);
        loadData(itemsCount);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        loadData(0);
    }

    @Override
    protected String cacheKey() {
        return "notice_comment";
    }

    @Override
    protected BaseResponse parseData(String data) {
        Gson gson = Global.getGson();
        return gson.fromJson(data, FeedListResponse.class);
    }

    @Override
    protected void addDataToList(BaseResponse response, boolean isNew) {
        FeedListResponse resp = (FeedListResponse) response;
        if (isNew) {
            mCommentAdapter.clear();
        }
        mCommentAdapter.append(resp.data);
        setHasMore(resp.has_more);
    }

    protected void loadData(final int start) {
        if (needLogin && Global.currentUser == null) {
            endRefreshing();
            showEmptyView();
            return;
        }
        new RequestServerTask<FeedListResponse>(FeedListResponse.class,NoticeCommentFragment.this,getString(R.string.please_wait_amoment)) {
            @Override
            protected String requestServer() {
                String url = String.format(NetworkConstants.NOTICE_COMMENT_URL, Global.getCurrentUid());
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("start", String.valueOf(start));
                params.put("limit", String.valueOf(Constants.PAGE_SIZE));
                if (start > 0 && lastResponseTime > 0) {
                    params.put("time", String.valueOf(lastResponseTime));
                }
                return HttpUtil.get(url, params);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                endRefreshing();
            }

            @Override
            protected void onSuccess(FeedListResponse result) {
                bindData(result, start == 0);
            }
        }
                .setSnackbarView(rootView)
                .start();
    }

    public class SimpleAdapter extends UltimateViewAdapter<SimpleViewHolder> {

        List<Feed> dataList;
        Object mParent;

        public SimpleAdapter(Object parent, List<Feed> dataList) {
            this.dataList = dataList;
            mParent = parent;
        }

        @Override
        public SimpleViewHolder getViewHolder(View view) {
            return new SimpleViewHolder(view, mParent, true);
        }

        @Override
        public SimpleViewHolder onCreateViewHolder(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.msg_list_item, parent, false);
            SimpleViewHolder vh = new SimpleViewHolder(v, mParent, false);
            return vh;
        }

        @Override
        public int getAdapterItemCount() {
            return dataList.size();
        }

        @Override
        public long generateHeaderId(int i) {
            return 0;
        }

        @Override
        public void onBindViewHolder(SimpleViewHolder holder, int position) {
            if (position < dataList.size()) {
                Feed feed = dataList.get(position);
                holder.bindData(feed);
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
            return null;
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

        }

        public void append(Feed item) {
            insert(dataList, item, dataList.size());
        }

        public void append(Feed[] items) {
            int pos = dataList.size();
            for (Feed item : items) {
                if (item.user != null || item.topic != null)
                    if (Global.getCurrentUid() != item.topic.user.uid) {
                        dataList.add(item);
                    }
            }
            notifyItemRangeInserted(pos, items.length);
        }

        public void remove(int position) {
            remove(dataList, position);
        }

        public void clear() {
            clear(dataList);
        }
    }

    public class SimpleViewHolder extends BaseViewHolder {

        @Bind(R.id.avatar)
        public CircleImageView avatar;

        @Bind(R.id.name)
        public TextView name;

        @Bind(R.id.date)
        public TextView date;

        @Bind(R.id.text)
        public TextView dataText;

        @Bind(R.id.notice_point)
        ImageView noticePoint;

        public SimpleViewHolder(View itemView, Object parent, boolean skip) {
            super(itemView, parent);
            if (skip)
                return;
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            addOnClickListener(avatar);
        }

        private Feed mFeed;


        public void bindData(Feed feed) {
            avatar.setBorderColor(Color.rgb(68,68,68));
            avatar.setBorderWidth(2);
            mFeed = feed;
            if (mFeed.r_status == Global.HAS_READ){
                noticePoint.setVisibility(View.INVISIBLE);
            }else {
                noticePoint.setVisibility(View.VISIBLE);
            }

            Picasso.with(getActivity()).load(feed.topic.user.pic).into(avatar);
            date.setText(Utils.getDatetimeStr(feed.topic.create_timestamp * 1000));
            dataText.setText(feed.topic.topic);

            SpannableString nameStr = feed.topic.user.getNameInFeed();
            nameStr.setSpan(new ForegroundColorSpan(Color.rgb(153,153,153)), 0, nameStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            SpannableString actionStr = new SpannableString(getString(R.string.comment_me));
            actionStr.setSpan(new RelativeSizeSpan(0.9f), 0, actionStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            name.setText(TextUtils.concat(nameStr, actionStr), TextView.BufferType.SPANNABLE);


        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.msg_view: {
                    new RequestServerTask<BaseResponse>(BaseResponse.class){
                        @Override
                        protected String requestServer() {
                            String url = String.format(NetworkConstants.MSG_HAS_READ_TOPIC,mFeed.id);
                            return HttpUtil.get(url,null);
                        }

                        @Override
                        protected void onSuccess(BaseResponse result) {

                        }
                    }.start();
                    if (mFeed.r_status == Global.NOT_READ) {
                        mFeed.r_status = Global.HAS_READ;
                        noticePoint.setVisibility(View.GONE);
                    }
                    Intent intent = new Intent(getActivity(), MarkDetailActivity.class);
                    intent.putExtra(Constants.FEED_EXTRA, mFeed.reply_topic.toString());
                    intent.putExtra(Constants.REPLY_USER, mFeed.topic.toString());
                    intent.putExtra(MarkDetailActivity.EXTRA_ORIG_TYPE, Constants.FEEDTYPE_USER_COMMENTS);
                    startActivity(intent);
                }
                break;
                case R.id.avatar:
                    ProfileActivity.openProfile(getActivity(), mFeed.topic.user.uid);
                    break;
            }
        }
    }*/
}
